//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include "ListCathCLF.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <iostream>
#include <fstream>
#include <stdexcept>

#include <zlib.h>
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
using namespace std;
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#define LENGTHLINE  5000
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
ListCathCLF::ListCathCLF() {
  
  this->namefile       = new String ();
  this->listDomainCode = new TListE <CathCLF> ();
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
ListCathCLF::ListCathCLF(const ListCathCLF& listCathCLF) {
  
  this->namefile       = new String (listCathCLF.namefile);
  this->listDomainCode = new TListE <CathCLF> (listCathCLF.listDomainCode);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
ListCathCLF::ListCathCLF(const ListCathCLF* listCathCLF) {
  
  this->namefile       = new String (listCathCLF->namefile);
  this->listDomainCode = new TListE <CathCLF> (listCathCLF->listDomainCode);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
ListCathCLF::~ListCathCLF() {
  
  if (this->namefile)       delete this->namefile;
  if (this->listDomainCode) delete this->listDomainCode;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void ListCathCLF::Namefile (String* namefile){
  
  if (namefile) this->namefile->In(namefile);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void ListCathCLF::ListDomainCode (TListE <CathCLF>* listDomainCode){
  
  if (listDomainCode) *(this->listDomainCode) = *(listDomainCode);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* ListCathCLF::Namefile (void){
  
  return this->namefile;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
TListE <CathCLF>* ListCathCLF::ListDomainCode (void) {
  
  return this->listDomainCode;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool ListCathCLF::LoadData (void) {
  
  if (!this->namefile || !this->namefile->Length()) return false;
  
  bool isGZFile = (this->namefile->Contain("gz"))?1:0;
  
  fstream file;
  gzFile  zipFile;
  
  char    buffer [LENGTHLINE + 1];
  String* data = new String ();
  
  if (isGZFile) zipFile = gzopen (this->namefile->Out(), "rb9");
  else          file.open (this->namefile->Out(), fstream::in);
  
  while (!((isGZFile)?gzeof(zipFile):file.eof())) {
    
    if (isGZFile) gzgets (zipFile, buffer, LENGTHLINE);
    else          file.getline (buffer, LENGTHLINE);
    
    if (!strlen(buffer) || (buffer[0] == '#')) continue;
    
    CathCLF* newCathCLF = new CathCLF ();

    if (newCathCLF->ReadData(buffer))
     this->listDomainCode->Add(newCathCLF);
    else 
      delete newCathCLF;
      
    
  }
  
  if (isGZFile) gzclose(zipFile);
  else          file.close();
  
  if (data)     delete data;
   
  return true;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
ListCathCLF ListCathCLF::operator= (const ListCathCLF& listCathCLF) {
  
  this->namefile->In(listCathCLF.namefile);
  
  *(this->listDomainCode) = *(listCathCLF.listDomainCode);  
  
  return *this;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
